#ifndef __THREAD__HPP__
#define __THREAD__HPP__
#include <pthread.h>
#include <functional>
#include <iostream>
#include <string>
namespace wxd
{
    
    static uint32_t number = 1;//直接写bug，要确保number++是原子的 
    //定义一个全局的锁
    pthread_mutex_t gmutex = PTHREAD_MUTEX_INITIALIZER;
    class Thread
    {
        using func_t = std::function<void()>;

    private:
        void EnableRunning()
        {
            _is_running = true;
        }
        void EnableDetach()
        {
            std::cout<<"线程 "<<_name<<" 分离了"<<std::endl;
            _is_detach = true;
        }
        static void *Routine(void *args)
        {
            Thread *self = static_cast<Thread *>(args);
            self->EnableRunning();
            if (self->_is_detach)
                    self->Detach();
            // 回调
            //std::cout << "线程"<<self->_name<<"开始执行！" << std::endl;
            //设置线程的名字
            pthread_setname_np(self->_tid,self->_name.c_str());
            self->_func();

            return nullptr;
        }
    public:
        Thread(func_t func)
            : _func(func), _is_running(false), _is_detach(false)
        {
            //调用构造的线程要加锁，确保++是原子的，防止多线程同时创建
            pthread_mutex_lock(&gmutex);
            _name = "pthread-"+std::to_string(number++);
            pthread_mutex_unlock(&gmutex);
        }



        bool Detach()
        {
            if (_is_detach)
            {
                return false;
            }
            if (_is_running)
            {
                int n = pthread_detach(_tid);
                if (n != 0)
                {
                    std::cerr << "detach "<<_name<<" fail" << std::endl;
                    return false;
                }
               
            }
            EnableDetach();
            return true;
        }

        bool start()
        {
            if (!_is_running)
            {
                int n = pthread_create(&_tid, nullptr, Routine, this);
                if (n != 0)
                {
                    std::cerr << "create "<<_name<<" error" << std::endl;
                    return false;
                }                
                return true;
            }
            else
            {
                return false;
            }
        }

        bool stop()
        {
            if (_is_running)
            {

                int n = pthread_cancel(_tid);
                if (n == 0)
                {
                    std::cout << "成功终止 "<<_name<< std::endl;
                    _is_running = false;
                    return true;
                }
                else
                {
                    std::cerr<<"stop "<<_name<<" error"<<std::endl;
                }
            }
            //std::cerr << "线程已经退出，无法再次stop" << std::endl;
            return false;
        }

        bool join()
        {
            // 没有detach才能进行join
            if (!_is_detach)
            {
                int n = pthread_join(_tid, nullptr);
                if (n == 0)
                {
                    std::cout << "join "<<_name<<" success" << std::endl;
                    return true;
                }
                else
                {
                    std::cerr << "join "<<_name<<" fail" << std::endl;
                    return false;
                }
            }
            else
            {
                std::cerr << "线程 "<<_name<<" 被detach，无法join" << std::endl;
                return false;
            }
        }
        std::string GetName()
        {
            return _name;
        }

        pthread_t Id()const
        {
            return _tid;
        }

    private:
        std::string _name;
        pthread_t _tid;
        bool _is_running;
        bool _is_detach;
        void *ret;
        func_t _func;
    };
}

#endif